//
//  WXComposePictureCollectionView.swift
//  weibo
//
//  Created by 王旭 on 16/4/26.
//  Copyright © 2016年 王旭. All rights reserved.

//自定义写微博配图视图

import UIKit
import SVProgressHUD

class WXComposePictureCollectionView: UICollectionView {
    
//   static var reuseID :String = "Compose_Picture_cell"
    
    var images : [UIImage] = []
    
    //添加图片cell点击闭包
    var pictureCollectionCellClick : (()->())?
 
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {

        //背景色
        backgroundColor = UIColor.whiteColor()
        
        //代理
        dataSource = self
        delegate = self
        //注册cell
        registerClass(WXComposePictureCollectionCell.self, forCellWithReuseIdentifier: "Compose_Picture_cell")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置每块大小
        let pictureCellMargin :CGFloat = 5
        let pictureCellWH : CGFloat = (frame.width - 2 * pictureCellMargin) / 3
        layout.itemSize = CGSize(width: pictureCellWH, height: pictureCellWH)
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
    }
    
//提供给外界方法给图片数组传图
    func addImage(image:UIImage) {
        if images.count < 9 {
            images.append(image)
            reloadData()
        }else{
            SVProgressHUD.showErrorWithStatus("达到数量上限")
        }
    }


}

//数据源方法
extension WXComposePictureCollectionView:UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.hidden = (self.images.count == 0)
        return self.images.count == 0 ? 0 : self.images.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Compose_Picture_cell", forIndexPath: indexPath) as! WXComposePictureCollectionCell
        //--------------------以下为测试代码-----------------
        //        cell.backgroundColor = RandomColor
        //--------------------以上为测试代码-----------------
        //传递数据
        
        if self.images.count == indexPath.item {
            cell.image = nil
        }else{
            cell.image = images[indexPath.item]
        }
        
        //给闭包赋值 1.cell->闭包 2.闭包->View 3.view->cell
        cell.closeButtonClickClosure = { [weak self] in
            self?.images.removeAtIndex(indexPath.item)
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if images.count == indexPath.item {
            pictureCollectionCellClick?()
        }
    }
}

//自定义cell
class WXComposePictureCollectionCell :UICollectionViewCell {
    
    //定义关闭按钮点击闭包
    var closeButtonClickClosure : (()->())?
    
    var image :UIImage?{
        didSet{
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add")
                closeButton.hidden = true
                
            }else{
                imageView.image = image
                closeButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(closeButton)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        closeButton.snp_makeConstraints { (make) -> Void in
            make.trailing.top.equalTo(contentView)
        }

    }
    
//关闭按钮点击事件
   @objc private func closeButtonClick() {
        closeButtonClickClosure?()
    }

//懒加载图片框
    private lazy var  imageView :UIImageView = UIImageView()
    
//懒加载关闭按钮
    private lazy var closeButton :UIButton = {
       let closeButton = UIButton()
        //点击事件
        closeButton.addTarget(self, action: "closeButtonClick", forControlEvents: .TouchUpInside)
        //设置图片
        closeButton.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return closeButton
    }()
}
